package com.ecgsystem.activity;

import java.util.concurrent.ConcurrentLinkedQueue;

import com.ecgsystem.constants.Constants;
import com.ecgsystem.panel.Panel6Online;
import com.ecgsystem.thread.ThreadGetSignal;

import com.ecgsystem.activity.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.FrameLayout.LayoutParams;

/**
 * TODO Put here a description of what this class does.
 *
 * @author ANHTUAN.
 *         Created Sep 24, 2012.
 */
public class OnlineECG extends Activity implements Constants {
	private static Panel6Online panel;
	private LinearLayout.LayoutParams panelParam;
	private ScrollView verticalView;
	private HorizontalScrollView horizotalView;
	private LinearLayout mainLayout;
	private LinearLayout internalWrapper;
	private ThreadGetSignal threadGetSignal;
	private static Context context;
	/**
	 * Hang doi chua tin hieu ecg
	 */
	public static ConcurrentLinkedQueue<Float> QueueDraw[];
	/**
	 * Co trang thai cho phep bat dau ve
	 */
	public static boolean startDraw = false;
	private static Thread mainLoop;

	@SuppressWarnings({ "cast", "unchecked" })
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContext(this.getApplicationContext());

		this.mainLayout = new LinearLayout(this);
		this.mainLayout.setOrientation(LinearLayout.VERTICAL);
		// cuon doc
		this.verticalView = new ScrollView(this);
		// cuon ngang
		this.horizotalView = new HorizontalScrollView(this);

		this.panelParam = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		this.panelParam.height = 525;
		this.panelParam.width = 1200;

		panel = new Panel6Online(this);
		panel.setLayoutParams(this.panelParam);
		panel.clearFocus();
		panel.setEnabled(false);
		// Tao man hinh cuon ngang
		this.internalWrapper = new LinearLayout(this);
		this.internalWrapper.setLayoutParams(new LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));

		this.horizotalView.addView(this.internalWrapper);

		this.internalWrapper.addView(panel);
		// chuyen dong mem mai
		this.horizotalView.setSmoothScrollingEnabled(true);
		// Tao man hinh cuon doc
		this.verticalView.addView(this.horizotalView, new ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));
		this.verticalView.setSmoothScrollingEnabled(true);
		this.verticalView.setBackgroundResource(R.drawable.ecg_background);

		setContentView(this.verticalView);

		QueueDraw = (ConcurrentLinkedQueue<Float>[]) new ConcurrentLinkedQueue[12];
		for (int i = 0; i < 12; i++) {
			QueueDraw[i] = new ConcurrentLinkedQueue<Float>();
		}
	}

	@Override
	public void onStart() {
		super.onStart();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("Events", "In onDestroy()");
		StopDraw();
		if (this.threadGetSignal != null && this.threadGetSignal.isAlive()) {
			this.threadGetSignal.destroy();
		}
	}

	/**
	 * Ve tin hieu ECG:
	 * Lay tin hieu va ve
	 *
	 */
	public void DrawSignal() {
		GetSignal();
		StartDraw();
	}

	/**
	 * Cho tin hieu den roi ve
	 *
	 */
	public void StartDraw() {
		mainLoop = new Thread() {
			@Override
			public void run() {
				try {
					while (!startDraw) {
						Thread.sleep(1000);
						// Log.d("Waitting", "Waitting");
					}
					panel.setQueueDraw(QueueDraw);
					panel.StartDraw();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		};
		mainLoop.start();
	}

	/**
	 * Dung thread ve
	 *
	 */
	public static void StopDraw() {
		startDraw = false;
		panel.Stop();
		try {
			mainLoop.join();
		} catch (InterruptedException exception) {
			// TODO Auto-generated catch-block stub.
			exception.printStackTrace();
		}
		/*if (mainLoop != null && mainLoop.isAlive()) {
			while (retry) {
				try {
					mainLoop.join();
					retry = false;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}*/
		mainLoop = null;
		if (ThreadGetSignal.ONLINE_STATUS == NO_WORKSTATION){
			Log.d("errrrrror", "No workstation");
		}
		else if(ThreadGetSignal.ONLINE_STATUS == STOP_SEND){
			Log.d("errrrrror", "Stop");		
		}else if(ThreadGetSignal.ONLINE_STATUS == CANNOT_CONNECT_SERVER){
			Log.d("errrrrror", "No connect");			
		}else{
			Log.d("errrrrror", "Error");			
		}
	}

	/**
	 * Khoi dong thread lay tin hieu realtime
	 *
	 */
	public void GetSignal() {
		this.threadGetSignal = new ThreadGetSignal();
		this.threadGetSignal.start();
	}
	
	@Override
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.threadGetSignal.sendStop();
			OnlineECG.StopDraw();
		}else if(keyCode == KeyEvent.KEYCODE_MENU){
			DrawSignal();
		}
		return true;
	}

	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @return  Context
	 */
	public static Context getContext() {
		return context;
	}

	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @param context
	 */
	public static void setContext(Context context) {
		OnlineECG.context = context;
	}
}
